package com.jiuyou.websocket.service;

import com.alibaba.fastjson.JSON;
import com.jiuyou.websocket.manager.WebSocketSessionManager;
import com.jiuyou.websocket.service.base.BaseService;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.storeUser.service.IStoreUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

/**
 * 登录websocket
 */

@Service
public class LoginWebSocketService extends BaseService {

    private final Logger log = LoggerFactory.getLogger(LoginWebSocketService.class);

    @Autowired
    private WebSocketSessionManager wsSessionManager;

    @Autowired
    private IMemberService memberService;
    @Autowired
    private IStoreUserService storeUserService;


    /**
     * 登录websocket 根据参数openid 获取到用户id
     * @param wsSession
     * @return
     */
    public boolean updateUserInfo(WebSocketSession wsSession) {

        Object user = getUser(wsSession);
        Long userId = getUserId(wsSession);
        String platform=getPlatform(wsSession);
        if (platform == null) {
            log.info("platform：null");
            return false;
        }
        String userKey=getUserKey(wsSession);
        if (userId != null && user != null && wsSessionManager.containsUserClient(userKey)) {
            return true;
        }

        String openId = getOpenId(wsSession);

        if (openId == null) {
            log.info("openId：null");
            return false;
        }

        if("store".equals(platform)){
            //门店
            StoreUser storeUser= storeUserService.selectStoreUserByOpenId(openId);
            if (storeUser == null) {
                log.error("【openId：{}】，用户不存在，无法查询数据！", openId);
                return false;
            }
            log.info("根据openId：{}，查询到用户：{}", openId, JSON.toJSONString(storeUser));

            userId = storeUser.getId();
            user=storeUser;
        }
        if("customer".equals(platform)){
            //用户
            Member member = memberService.selectMemberByOpenId(openId);
            if (member == null) {
                log.error("【openId：{}】，用户不存在，无法查询数据！", openId);
                return false;
            }
            log.info("根据openId：{}，查询到用户：{}", openId, JSON.toJSONString(member));

            userId = member.getId();
            user=member;
        }

        //查询用户

        //放入session
        wsSession.getAttributes().put("userId", userId);
        wsSession.getAttributes().put("user", user);
        wsSession.getAttributes().put("platform", platform);

        //保存客户端session
        log.info("更新到WebSession，userId：{}，member：{}", userId, JSON.toJSONString(getUser(wsSession)));
        wsSessionManager.addUserClient(userKey, wsSession);
        return true;
    }
}
